package com.itheima.course.design.dao;

import com.itheima.course.design.entity.Course;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CourseDao {
    // 获取所有课程
    public List<Course> getAllCourses() {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        List<Course> courses = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            stmt = conn.createStatement();
            String sql = "SELECT * FROM courses";
            rs = stmt.executeQuery(sql);

            while (rs.next()) {
                Course course = new Course();
                course.setId(rs.getString("id"));
                course.setCourseCode(rs.getString("course_code"));
                course.setName(rs.getString("name"));
                course.setInstructor(rs.getString("instructor"));
                course.setMaxStudents(rs.getInt("max_students"));
                courses.add(course);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, stmt, conn);
        }

        return courses;
    }

    // 根据课程代码查询课程
    public Course getCourseByCode(String courseCode) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Course course = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM courses WHERE course_code = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, courseCode);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                course = new Course();
                course.setId(rs.getString("id"));
                course.setCourseCode(rs.getString("course_code"));
                course.setName(rs.getString("name"));
                course.setInstructor(rs.getString("instructor"));
                course.setMaxStudents(rs.getInt("max_students"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(rs, pstmt, conn);
        }

        return course;
    }

    // 添加新课程
    public boolean addCourse(Course course) {
        Connection conn = null;
        PreparedStatement pstmt = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "INSERT INTO courses (course_code, name, instruct  qor, max_students) VALUES (?, ?, ?, ?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, course.getCourseCode());
            pstmt.setString(2, course.getName());
            pstmt.setString(3, course.getInstructor());
            pstmt.setInt(4, course.getMaxStudents());

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            DBUtil.close(pstmt, conn);
        }
    }
}